{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8976d6ca",
   "metadata": {},
   "source": [
    "## 第九周学习笔记"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0f290b0",
   "metadata": {},
   "source": [
    "> * 购物车项目的完善（接第八周）\n",
    "> * 元组 & 字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "710ebb16",
   "metadata": {},
   "source": [
    "### 课堂练习： list 实践\n",
    "\n",
    "> 购物车项目：\n",
    "> 要求：\n",
    ">> * 1. 运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    ">> * 2. 让用户输入商品编号进行商品的购买。\n",
    ">> * 3. 用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    ">> * 4. 用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "c27aeb9b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的余额：12345678\n",
      "------商品列表信息------\n",
      "商品编号：0， 商品的名称：python课本， 商品的价格：60 元\n",
      "商品编号：1， 商品的名称：华为手机， 商品的价格：8000 元\n",
      "商品编号：2， 商品的名称：小米手环， 商品的价格：1990 元\n",
      "商品编号：3， 商品的名称：蜜雪冰城， 商品的价格：8 元\n",
      "请输入正确的商品编号进行购买(如果退出请输入“q”): 2\n",
      "【小米手环】已经加入您的购物车，您的余额还有 【12343688】 元\n",
      "------商品列表信息------\n",
      "商品编号：0， 商品的名称：python课本， 商品的价格：60 元\n",
      "商品编号：1， 商品的名称：华为手机， 商品的价格：8000 元\n",
      "商品编号：2， 商品的名称：小米手环， 商品的价格：1990 元\n",
      "商品编号：3， 商品的名称：蜜雪冰城， 商品的价格：8 元\n",
      "请输入正确的商品编号进行购买(如果退出请输入“q”): q\n",
      "您的购物车： [['小米手环', 1990]] \n",
      " 您的账户余额：12343688 元\n"
     ]
    }
   ],
   "source": [
    "# 1.运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "# 商品列表数据\n",
    "goods_list = [\n",
    "    # 商品的样式： [商品名称，商品价格],\n",
    "    [\"python课本\",60],\n",
    "    [\"华为手机\",8000],\n",
    "    [\"小米手环\",1990],\n",
    "    [\"蜜雪冰城\",8]\n",
    "]\n",
    "# 写到某一步，发现前面需要内容。可以优化增加\n",
    "user_shopping_cart = [] # 用户列表初始为空，后续用 append 进行增加\n",
    "\n",
    "# 用户输入：input\n",
    "Alipay = int(input(\"请输入您的余额：\"))\n",
    "\n",
    "    # 打印商品信息，打印商品的次数是由商品列表的长度决定的 (len(goods_list))\n",
    "while True: # 将 实现 过程的循环\n",
    "    print(\"------商品列表信息------\")\n",
    "    for i in range(len(goods_list)):\n",
    "        # 给格式化的样式（先给）\n",
    "        # 样式（不懂代码的人可以完成的工作）\n",
    "        #      ：\"商品编号：xx，商品的名称：xx，商品的价格：xx\"\n",
    "        print(\"商品编号：%s， 商品的名称：%s， 商品的价格：%s 元\" %(i,goods_list[i][0],goods_list[i][1]))\n",
    "\n",
    "\n",
    "    # 2.让用户输入商品编号进行商品的购买。\n",
    "\n",
    "    # 用户输入商品编号\n",
    "    choice = input(\"请输入正确的商品编号进行购买(如果退出请输入“q”): \")\n",
    "    if choice.isdigit(): # isdigit 判断字符串是否是数值\n",
    "        choice = int(choice) # 如果是数值，把它变成真正的 int\n",
    "        # 判断商品编号是否在 我们的范围内\n",
    "        if choice >= len(goods_list):\n",
    "            print(\"【您输入的商品不存在，请检查再次输入~】\")\n",
    "        else:\n",
    "            # 对比商品编号，找到对应商品：goods_list[choice][1]，\n",
    "            # 1.检查用户余额 VS 商品价格关系\n",
    "            if Alipay > goods_list[choice][1]:\n",
    "                # 买得起 1. 扣钱  2. 把商品加到用户购物列表里\n",
    "                Alipay  = Alipay - goods_list[choice][1]\n",
    "                user_shopping_cart.append(goods_list[choice])\n",
    "                print(\"【%s】已经加入您的购物车，您的余额还有 【%s】 元\" %(goods_list[choice][0],Alipay))\n",
    "            else: # 买不起\n",
    "                print(\"您的余额不足以购买，请更换支付方式\")\n",
    "    elif choice == \"q\":\n",
    "        # 如果输入 q ,则说明用户要退出购买了，我们将打印用户已购买的 商品列表，以及 用户余额\n",
    "        # 利用'\\n'可以实现换行\n",
    "        print(\"您的购物车：\",user_shopping_cart,'\\n',\"您的账户余额：%s 元\"%(Alipay))\n",
    "        break\n",
    "    else:\n",
    "        print(\"请输入正确的【商品编码】！！或者按照输入提示进行输入\")\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b85003d1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6197d69c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "7fb72545",
   "metadata": {},
   "source": [
    "--------------\n",
    "### 元组（tuple）\n",
    "\n",
    "* [元组]()\n",
    "> 1. 它是list几乎是完全一样的序列数据\n",
    "> 2. 它与list不同的是，**不可变**（因为不可变，所以几乎没有方法）\n",
    "> 3. 元组的形式：（1，2，3，4，5） 对比 元组（） 列表[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "62562dcc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('10001', '10002', '10003', '10001') <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "stu_id = \"10001\",\"10002\",\"10003\",\"10001\"\n",
    "print(stu_id,type(stu_id))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "465c8e8f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 元组第一种方法，查询元素出现的次数\n",
    "stu_id.count(\"10001\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "45500b9a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 元组第二种方法，索引元素\n",
    "stu_id.index(\"10002\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c06614d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=10, tm_mday=25, tm_hour=16, tm_min=21, tm_sec=33, tm_wday=0, tm_yday=298, tm_isdst=0)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e463da41",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8ef0bc6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "a7524602",
   "metadata": {},
   "source": [
    "-------------\n",
    "### 字典\n",
    "\n",
    "> 1. 创建字典的三种常见的方法\n",
    ">>  1 . 直接按照字典的格式创建 ： {key1:value1 , key2:value2 ,.....}\n",
    ">>  2 . dict()函数来创建字典： dict(key1=value1,key2=value2,...)\n",
    ">>  3 . 字典数据增加的方式： 新建空字典 dict_name = {} , dict_name[key]=value1 ... \n",
    "> 2. 字典的性质 dict是无序的 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "4dd0ab45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu_10001': 'Alex', 'stu_10002': 'Jack', 'stu_10003': 'Marry'}\n"
     ]
    }
   ],
   "source": [
    "# 1.直接按照字典的格式创建 ： {key1:value1 , key2:value2 ,.....}\n",
    "stu_info = {\n",
    "    'stu_10001':\"Alex\" , \n",
    "    'stu_10002':\"Jack\" ,\n",
    "    'stu_10003':\"Marry\" \n",
    "}\n",
    "print(stu_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "3359ed6b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu_10001': 'Alex', 'stu_10002': 'Jack', 'stu_10003': 'Marry'}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2. dict()函数来创建字典: dict(key1=value1,key2=value2,...)\n",
    "stu_info_02 = dict(\n",
    "    stu_10001 = \"Alex\",\n",
    "    stu_10002 =\"Jack\" ,\n",
    "    stu_10003 =\"Marry\" \n",
    ")\n",
    "stu_info_02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "e6f56a93",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu_10001': 'Alex', 'stu_10002': 'Jack', 'stu_10003': 'Marry'}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3. 字典数据增加的方式： 新建空字典 dict_name = {} , dict_name[key]=value1 ... \n",
    "stu_info_03 = {}\n",
    "stu_info_03[\"stu_10001\"] = \"Alex\"\n",
    "stu_info_03[\"stu_10002\"] = \"Jack\"\n",
    "stu_info_03[\"stu_10003\"] = \"Marry\"\n",
    "stu_info_03\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e205a792",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_info_03"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f16573e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_info_03."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30303f0c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25c5d32c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "a374d707",
   "metadata": {},
   "source": [
    "## 课后练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a2348702",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu2001': 'red', 'stu2002': 'green', 'stu2003': 'blue'}\n"
     ]
    }
   ],
   "source": [
    "# 创建字典的常用办法 一： 变量 = {key1:value1,key2:value2 , key3:value3,...}\n",
    "\n",
    "info = {\n",
    "    \"stu2001\":\"red\",\n",
    "    \"stu2002\":\"green\",\n",
    "    \"stu2003\":\"blue\"\n",
    "}\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f535fc9f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu2001': 'red', 'stu2002': 'green', 'stu2003': 'blue'}\n"
     ]
    }
   ],
   "source": [
    "# 创建字典的常用办法 二：利用 dict() 函数来创建字典\n",
    "#                         dict =(key1 = value1,key2=value2,...)\n",
    "#                        跟第一个办法的区别 1.利用dict()函数\n",
    "#                                           2.key 值和 value 值中间用 = \n",
    "\n",
    "info2 = dict(\n",
    "         stu2001=\"red\",\n",
    "         stu2002=\"green\",\n",
    "         stu2003=\"blue\"\n",
    ")\n",
    "print(info2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f932429b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu2001': 'red', 'stu2002': 'green', 'stu2003': 'blue'}\n"
     ]
    }
   ],
   "source": [
    "# 创建字典的常用办法(字典数据增加的方式) 三： 新建空字典 dict_name ={ },\n",
    "#                         dict_name[key]=value1 ...\n",
    "\n",
    "\n",
    "info3 = { }\n",
    "info3[\"stu2001\"] = \"red\"\n",
    "info3[\"stu2002\"] = \"green\"\n",
    "info3[\"stu2003\"] = \"blue\"\n",
    "print(info)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "13ab61ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu2001': 'xiaohong', 'stu2002': 'xiaoming', 'stu2003': 'xiaoli'}\n"
     ]
    }
   ],
   "source": [
    "test = {\n",
    "    \"stu2001\" : \"xiaohong\",\n",
    "    \"stu2002\" : \"xiaoming\",\n",
    "    \"stu2003\" : \"xiaoli\"\n",
    "}\n",
    "print(test)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7ea6552",
   "metadata": {},
   "source": [
    "### 增加字典数据\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c5295fd2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu2001': 'xiaohong', 'stu2002': 'xiaoming', 'stu2003': 'xiaoli', 'stu2004': 'xiaozhang'}\n"
     ]
    }
   ],
   "source": [
    "test[\"stu2004\"] = \"xiaozhang\"\n",
    "print(test)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "198a8910",
   "metadata": {},
   "source": [
    "### 修改字典数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9b05d86b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu2001': 'xiaoliu', 'stu2002': 'xiaoming', 'stu2003': 'xiaoli', 'stu2004': 'xiaozhang'}\n"
     ]
    }
   ],
   "source": [
    "test[\"stu2001\"] = \"xiaoliu\"\n",
    "print(test)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f2dc7e3",
   "metadata": {},
   "source": [
    "### 删除字典数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "7c6aa22c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu2004': 'xiaozhang'}\n"
     ]
    }
   ],
   "source": [
    "# 1.利用 .pop() 来删除 这是字典中标准的删除办法\n",
    "\n",
    "test.pop(\"stu2001\")\n",
    "print(test)\n",
    "\n",
    "# test.pop(\"xiaoliu\")\n",
    "# print(test)\n",
    "\n",
    "# test.pop( )\n",
    "# print(test)\n",
    "\n",
    "# 经过上面的尝试，可知 1. .pop()括号里面需要输入的是 key\n",
    "#                      2. 与列表中的 .pop() 相比，字典中的括号里面必须填写内容\n",
    "#                         而列表中的括号 可以不填写内容，其会自动删除末位！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "739972e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu2001': 'xiaohong', 'stu2002': 'xiaoming', 'stu2003': 'xiaoli'}\n"
     ]
    }
   ],
   "source": [
    "test1= {\n",
    "    \"stu2001\" : \"xiaohong\",\n",
    "    \"stu2002\" : \"xiaoming\",\n",
    "    \"stu2003\" : \"xiaoli\"\n",
    "}\n",
    "print(test1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "f6533e5d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.利用 del 函数进行删除 但是 del 不是 python 中常用的删除方式\n",
    "# 需要注意的点是：1.del 变量 [key]\n",
    "#                 2.del 键不仅能删除字典的中的某一个值，还能删除字典\n",
    "#                   但值得一提的是，在执行了del的操作后，字典将不复存在！\n",
    "#                 3. 在列表 del的操作中 其 del list[ ] 括号里面填的是元素对应的索引值  \n",
    "\n",
    "\n",
    "\n",
    "# del test1[\"stu2003\"]   # 删除字典中的某个值 括号里面一定要填 key（键）\n",
    "# print(test1)\n",
    "\n",
    "# test1.clear()     # 清空字典\n",
    "# print(test1)\n",
    "\n",
    "# del test1\n",
    "# print(test1)  # 删除字典 因为字典的已经删除 所以就 print（打印）不出来了\n",
    "                # 系统会显示字典不存在\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "9ad4eacb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu2001': 'xiaohong', 'stu2002': 'xiaoming', 'stu2003': 'xiaoli'}\n",
      "{'stu2001': 'xiaohong', 'stu2002': 'xiaoming'}\n"
     ]
    }
   ],
   "source": [
    "# 3.随机删除 利用 .popitem() 函数\n",
    "# ps：随机返回并删除字典中的最后一对键和值。\n",
    "test2 = {\n",
    "    \"stu2001\" : \"xiaohong\",\n",
    "    \"stu2002\" : \"xiaoming\",\n",
    "    \"stu2003\" : \"xiaoli\"\n",
    "\n",
    "}\n",
    "print(test2)\n",
    "\n",
    "test2.popitem()\n",
    "print(test2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce4dc31c",
   "metadata": {},
   "source": [
    "###  查找字典数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "629ab87a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu2001': 'xiaohong', 'stu2002': 'xiaoming', 'stu2003': 'xiaoli'}\n",
      "False\n",
      "姓名： xiaohong\n",
      "姓名： xiaoming\n"
     ]
    }
   ],
   "source": [
    "test3 = {\n",
    "    \"stu2001\" : \"xiaohong\",\n",
    "    \"stu2002\" : \"xiaoming\",\n",
    "    \"stu2003\" : \"xiaoli\"\n",
    "}\n",
    "print(test3)\n",
    "\n",
    "# 1.标准方法\n",
    "print(\"stu2004\" in test3)  # 查询某个键 key 是否在字典当中\n",
    "print(\"姓名：\",test3[\"stu2001\"]) # 获取键值 注意区别下面另一种获取键值的办法\n",
    "print(\"姓名：\",test3.get(\"stu2002\")) # 获取键值的另一种方法\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21efcded",
   "metadata": {},
   "source": [
    "### 研究字典特性！！！"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f93b5a3",
   "metadata": {},
   "source": [
    "* [key 是唯一的！](https://www.runoob.com/python3/python3-dictionary.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "47a5ba96",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名:  xiaoming \n",
      " 年龄： 20\n"
     ]
    }
   ],
   "source": [
    "# 不允许同一个键出现两次。创建时如果同一个键被赋值两次，后一个值会被记住，如下实例：\n",
    "\n",
    "test4 = {\n",
    "    \"Name\" : \"xiaohong\",\n",
    "    \"Age\" : \"20\",\n",
    "    \"Name\" : \"xiaoming\"\n",
    "}\n",
    "print (\"姓名: \", test4['Name'],'\\n',\"年龄：\",test4[\"Age\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93b3beb3",
   "metadata": {},
   "source": [
    "* [key 不可变性！](https://www.runoob.com/python3/python3-dictionary.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10610724",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 键必须不可变，所以可以用数字，字符串或元组充当，而用列表就不行，如下实例：\n",
    "test5 = {['Name']: 'Runoob', 'Age': 7}\n",
    " \n",
    "print (\"test5['Name']: \", test5['Name'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9f7bbdc",
   "metadata": {},
   "source": [
    "* dict 是无序的！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "494aac9c",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "0",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-75-41812ef6eb75>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[1;34m\"job\"\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;34m\"11\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m }\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 0"
     ]
    }
   ],
   "source": [
    "# 经下列代码可知，字典无法用下标来进行索引，会报错\n",
    "\n",
    "# name = {\n",
    "#     \"age\": \"10\",\n",
    "#     \"job\": \"11\"\n",
    "# }\n",
    "# print(name[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5503f503",
   "metadata": {},
   "source": [
    "### 字典遍历"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "556d9757",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_01 = {\n",
    "    \"stu2001\" : \"xiaohong\",\n",
    "    \"stu2002\" : \"xiaoming\",\n",
    "    \"stu2003\" : \"xiaoli\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6b2ea3f1",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu2001': 'xiaohong', 'stu2002': 'xiaoming', 'stu2003': 'xiaoli'}\n"
     ]
    }
   ],
   "source": [
    "print(stu_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "babf84af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu2001\n",
      "stu2002\n",
      "stu2003\n"
     ]
    }
   ],
   "source": [
    "# 遍历一个字典\n",
    "# i 是 item 在一个字典中是什么？\n",
    "\n",
    "for i in stu_01:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5979ec80",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu2001 xiaohong\n",
      "stu2002 xiaoming\n",
      "stu2003 xiaoli\n"
     ]
    }
   ],
   "source": [
    "# 循环遍历整个字典的 items\n",
    "# 对许多相同的代码进行同样的操作\n",
    "\n",
    "for k,v in stu_01.items():\n",
    "    print(k,v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9178a92e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'2001': 'xiaohong', '2002': 'xiaoming', '2003': 'xiaoli'}\n"
     ]
    }
   ],
   "source": [
    "# 实践1：stu2001 中 取出数字，作为新的字典 key\n",
    "stu_02 = {}\n",
    "for k,v in stu_01.items():\n",
    "    stu_02[k[3:]] = v  # 用字典新增的方式创建字典\n",
    "print(stu_02)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "904270f8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'2001': 'XIAOHONG', '2002': 'XIAOMING', '2003': 'XIAOLI'}\n"
     ]
    }
   ],
   "source": [
    "# 实践2: value 我也想变， 所有字母大写\n",
    "stu_02 = {}\n",
    "for k,v in stu_01.items():\n",
    "    stu_02[k[3:]] = v.upper()\n",
    "print(stu_02)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "0a1dfccb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{2001: 'xiaohong', 2002: 'xiaoming', 2003: 'xiaoli'}\n"
     ]
    }
   ],
   "source": [
    "# 实践3：key 由 ‘str’变成 int\n",
    "stu_03 = {}\n",
    "for k,v in stu_01.items():\n",
    "    stu_03[int(k[3:])] = v\n",
    "print(stu_03)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a0a72427",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0dd24be3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
